package com.jn.erp.wechat.common.api.controller;

import com.jn.erp.common.core.result.AjaxResult;
import com.jn.erp.security.entity.SysUser;
import com.jn.erp.wechat.common.handler.cp.CpDepartmentHandler;
import com.jn.erp.wechat.common.handler.cp.CpExternalHandler;
import com.jn.erp.wechat.common.handler.cp.CpMessageHandler;
import com.jn.erp.wechat.common.handler.cp.CpUserHandler;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * <pre>
 *  企业微信交互信息
 * </pre>
 *
 * @author sunjiyong
 * @since 2024/9/10 9:50
 */
@AllArgsConstructor
@RestController
@RequestMapping("/wx/wecom")
public class CpController {

    private final CpDepartmentHandler departmentHandler;
    private final CpUserHandler cpUserHandler;
    private final CpMessageHandler cMessageHandler;
    private final CpExternalHandler cpExternalHandler;


    //--------------------------------部门相关业务------------------------------
    //新建机构
    @GetMapping("/deptAdd")
    public AjaxResult deptAdd()   {
        Boolean op = departmentHandler.createDeptById(5L);
        if(op){
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }
    //新建机构
    @GetMapping("/deptDelete")
    public AjaxResult deptDelete()   {
        Boolean op = departmentHandler.deleteDeptById(5L);
        if(op){
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }
    //--------------------------------用户相关业务------------------------------

    //用户信息同步-添加用户
    @GetMapping("/userAdd")
    public AjaxResult userAdd()   {
        SysUser sysUser=new SysUser();
        sysUser.setName("sunjiyong");
        sysUser.setGender(1);
        sysUser.setMobile("15840142750");
        sysUser.setEmail("315541219@qq.com");
        sysUser.setNickName("测试API用户");
        sysUser.setOrgId(5l);
        return AjaxResult.success(cpUserHandler.create(sysUser));
    }

    //用户信息同步-删除用户
    @GetMapping("/userDelete")
    public AjaxResult userDelete()   {
        SysUser sysUser=new SysUser();
        sysUser.setName("sunjiyong");
        return AjaxResult.success(cpUserHandler.delete(sysUser));
    }

    //--------------------------------消息相关业务------------------------------

    //机构内消息推送
    @GetMapping("/sendSubscribeMsg")
    public AjaxResult sendSubscribeMsg()   {
            cMessageHandler.TextMessageParty("5");
            cMessageHandler.TextMessageUsers("sunjiyong");
            return AjaxResult.success();
    }

    //客户消息推送接口测试
    @GetMapping("/sendSubscribeMsg2")
    public AjaxResult sendSubscribeMsg2()   {
        cpExternalHandler.getQrcodeContactWay("0f2d60867d0f85b65af489d57a869339");
        cpExternalHandler.contactList("sunjiyong");
//        cpExternalHandler.addContactWay();
//        cpExternalHandler.contactList();
////        cMessageHandler.TextMessageParty();
////        cMessageHandler.TextMessageUsers();
////        cMessageHandler.SendLinkedCorpMessage();
//        cMessageHandler.GroupRobotMessage();
////        cpExternalHandler.contactMsg();
            return AjaxResult.success();
    }





//
//
//    /**
//     * <pre>
//     * 获取用户信息接口
//     * </pre>
//     */
//    @GetMapping("/info")
//    public String info(String sessionKey,
//                       String signature, String rawData, String encryptedData, String iv) {
//        String appid = MiniappService.getDefaultAppId();
//
//        if (!wxMaService.switchover(appid)) {
//            throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置，请核实！", appid));
//        }
//
//        // 用户信息校验
//        if (!wxMaService.getUserService().checkUserInfo(sessionKey, rawData, signature)) {
//            WxMaConfigHolder.remove();//清理ThreadLocal
//            return "user check failed";
//        }
//
//        // 解密用户信息
//        WxMaUserInfo userInfo = wxMaService.getUserService().getUserInfo(sessionKey, encryptedData, iv);
//        WxMaConfigHolder.remove();//清理ThreadLocal
//        userInfo.
//        return JsonUtils.toJson(userInfo);
//    }
}
